home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / Buffer.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  7.5 KB  |  232 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Buffer.h
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __BUFFER__
  15. #define __BUFFER__
  16.  
  17. #ifndef __MEMORY__
  18. #include "Memory.h"
  19. #endif
  20.  
  21. #ifndef    __DIRECTOBJECT__
  22. #include "DirectObject.h"
  23. #endif
  24.  
  25. #pragma push
  26. #pragma segment Buffer
  27.  
  28. class ostream;
  29. class ABuffer;
  30.  
  31. extern short CompareBuffers ( const void* a, unsigned long alen, const void* b, unsigned long blen );
  32. inline unsigned long Minimum ( unsigned long a, unsigned long b ) { return a < b ? a : b; }
  33.  
  34. /*============================================================================
  35.  
  36.     ABuffer is a abstract class representing a buffer, a chunk of
  37.     contiguous RAM which has a physical length and a starting address.
  38.  
  39. ----------------------------------------------------------------------------*/
  40.  
  41. class ABuffer : public TDirectObject
  42. {
  43. public:
  44.  
  45.     virtual    ~ABuffer ();
  46.             ABuffer&                operator = ( const ABuffer& );
  47.  
  48.                                     operator const void* () const;
  49.  
  50. // subclasses must implement these…
  51.     virtual    const void*                GetPhysicalStart () const = 0;
  52.     virtual    unsigned long            SetPhysicalLength ( unsigned long ) = 0;
  53.     virtual    unsigned long            GetPhysicalLength () const = 0;
  54.  
  55. // comparison methods
  56.     virtual    Boolean                    operator == ( const ABuffer& ) const;
  57.     virtual    Boolean                    operator != ( const ABuffer& ) const;
  58.  
  59. // safe copying methods
  60.     virtual    unsigned long            ReadFrom ( const void* source, unsigned long length );
  61.     virtual    unsigned long            WriteTo ( void* dest, unsigned long length ) const;
  62.  
  63. // content setting methods
  64.     virtual    void                    ZeroBuffer ();
  65.     virtual    void                    FillBuffer ( unsigned char );
  66.  
  67. // debugging
  68.     virtual    ostream&                operator >> ( ostream& ) const;
  69.  
  70. protected:    ABuffer ();
  71. };
  72.  
  73. /*============================================================================
  74.  
  75.     CBuffer is a concrete class representing a buffer.
  76.  
  77.     The implementation of this class is smart; i.e. if the buffer logical
  78.     length is no greater than 7 bytes, then the buffer data is stored
  79.     inside the object, that keeps us from senselessly allocating teensy
  80.     blocks on the heap since we have space in the object already.
  81.  
  82. ----------------------------------------------------------------------------*/
  83.  
  84. class CBuffer : public ABuffer
  85. {
  86. public:        CBuffer ();
  87.             CBuffer ( unsigned long length );
  88.             CBuffer ( const void* source, unsigned long length );
  89.             CBuffer ( const ABuffer& );
  90.             CBuffer ( const CBuffer& );
  91.     virtual    ~CBuffer ();
  92.             CBuffer&                operator = ( const CBuffer& );
  93.  
  94.     virtual    const void*                GetPhysicalStart () const;
  95.     virtual    unsigned long            SetPhysicalLength ( unsigned long );
  96.     virtual    unsigned long            GetPhysicalLength () const;
  97.  
  98.     virtual    ostream&                operator >> ( ostream& ) const;
  99.  
  100. private:
  101.             unsigned char            fIsExternal;
  102.  
  103.             struct External
  104.             {
  105.                 unsigned long        fLength;
  106.                 void*                fBuffer;
  107.             };
  108.  
  109. #define    kBufferMaxInternalLength ( sizeof ( External ) -  sizeof ( unsigned char ) )
  110.  
  111.             struct Internal
  112.             {
  113.                 unsigned char        fLength;
  114.                 unsigned char        fBuffer [ kBufferMaxInternalLength ];
  115.             };
  116.  
  117.             union
  118.             {
  119.                 External            fExternal;
  120.                 Internal            fInternal;
  121.             } fType;
  122. };
  123.  
  124. /*============================================================================
  125.  
  126.     CPrefixBuffer is a concrete class which represents a buffer
  127.     which creates a new buffer containing a length prefix from an existing
  128.     buffer. The length prefix can be a 1, 2, or 4 byte unsigned integer.
  129.  
  130.     The length prefix buffer will be larger than the original buffer by
  131.     either 1, 2, or 4 bytes, and this prefix will contain an unsigned
  132.     integer representing the length of the original buffer.
  133.  
  134.     When the length buffer is destructed, it can also write its contents
  135.     back to the original buffer, this option is selected in the constructor
  136.     argument list parameter “updateSource”.
  137.  
  138.     For example, this class can be used to easily create a pascal string
  139.     copy of a null terminated string:
  140.  
  141.         char s [] = "Hello World!!";
  142.         CPrefixBuffer b ( s, strlen ( s ), sizeof ( char ) );
  143.         char* t = (char*) b.GetPhysicalStart ();
  144.  
  145.     The data pointed to by <t> would look like:
  146.  
  147.          $0C  $48 $65 $6c $6c $6f $20 $57 $6f $72 $6c $64 $21 $21
  148.          ‘\n’ ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’ ‘!’ ‘!’
  149.  
  150. ----------------------------------------------------------------------------*/
  151.  
  152. class CPrefixBuffer : public ABuffer
  153. {
  154. public:
  155.  
  156.     enum Prefix { kByte = 1, kWord = 2, kLong = 4 };
  157.  
  158.             CPrefixBuffer ( const ABuffer&, Prefix = kByte );
  159.             CPrefixBuffer ( ABuffer&, Boolean update, Prefix = kByte );
  160.             CPrefixBuffer ( const void*, unsigned long, Prefix = kByte );
  161.             CPrefixBuffer ( Prefix, unsigned long logicalSize );
  162.     virtual    ~CPrefixBuffer ();
  163.  
  164.     virtual    const void*                GetLogicalStart () const;
  165.     virtual    unsigned long            SetLogicalLength ( unsigned long );
  166.     virtual    unsigned long            GetLogicalLength () const;
  167.  
  168.     virtual    const void*                GetPhysicalStart () const;
  169.     virtual    unsigned long            SetPhysicalLength ( unsigned long );
  170.     virtual    unsigned long            GetPhysicalLength () const;
  171.  
  172.     virtual    void                    SetSource ( ABuffer* );
  173.             const ABuffer*            GetSource () const;
  174.  
  175.     virtual    void                    SetUpdate ( Boolean );
  176.             Boolean                    GetUpdate () const;
  177.     virtual    unsigned long            UpdateNow ();
  178.  
  179.     virtual    ostream&                operator >> ( ostream& ) const;
  180.  
  181. protected:    void                    UpdatePrefixWithLength ( unsigned long logicalLength );
  182.  
  183. private:    CBuffer                    fBuffer;
  184.             ABuffer*                fSource;
  185.             Prefix                    fPrefix;
  186.             Boolean                    fUpdate;
  187. };
  188.  
  189. /*============================================================================
  190.  
  191.     CWrapperBuffer is a concrete buffer class which wraps a non-object
  192.     chunk of nonrelocatable memory.
  193.  
  194. ----------------------------------------------------------------------------*/
  195.  
  196. class CWrapperBuffer : public ABuffer
  197. {
  198. public:        CWrapperBuffer ( void* source, unsigned long length );
  199.     virtual    ~CWrapperBuffer ();
  200.  
  201.     virtual    const void*                GetPhysicalStart () const;
  202.     virtual    unsigned long            SetPhysicalLength ( unsigned long );
  203.     virtual    unsigned long            GetPhysicalLength () const;
  204.  
  205. private:    void*                    fSource;
  206.             unsigned long            fLength;
  207. };
  208.  
  209. /***********************************|****************************************/
  210.  
  211. inline ABuffer::operator const void* () const { return GetPhysicalStart (); }
  212. inline void ABuffer::ZeroBuffer () { FillBuffer ( 0 ); }
  213. inline Boolean ABuffer::operator != ( const ABuffer& that ) const { return !operator == ( that ); }
  214. inline const void* CBuffer::GetPhysicalStart () const { return fIsExternal ? fType.fExternal.fBuffer : fType.fInternal.fBuffer; }
  215. inline unsigned long CBuffer::GetPhysicalLength () const { return fIsExternal ? fType.fExternal.fLength : fType.fInternal.fLength; }
  216. inline const void* CPrefixBuffer::GetPhysicalStart () const { return fBuffer.GetPhysicalStart (); }
  217. inline const void* CPrefixBuffer::GetLogicalStart () const { return (char*) fBuffer.GetPhysicalStart () + fPrefix; }
  218. inline unsigned long CPrefixBuffer::GetPhysicalLength () const { return fBuffer.GetPhysicalLength (); }
  219. inline unsigned long CPrefixBuffer::SetPhysicalLength ( unsigned long length ) { return fBuffer.SetPhysicalLength ( length ); }
  220. inline void CPrefixBuffer::SetSource ( ABuffer* source ) { fSource = source; }
  221. inline const ABuffer* CPrefixBuffer::GetSource () const { return fSource; }
  222. inline void CPrefixBuffer::SetUpdate ( Boolean update ) { fUpdate = update; }
  223. inline Boolean CPrefixBuffer::GetUpdate () const { return fUpdate; }
  224. inline const void* CWrapperBuffer::GetPhysicalStart () const { return fSource; }
  225. inline unsigned long CWrapperBuffer::GetPhysicalLength () const { return fLength; }
  226.  
  227. /***********************************|****************************************/
  228.  
  229. #pragma pop
  230.  
  231. #endif    // __BUFFER__
  232.